/*
XCommander Extension
http://www.xcommander.ch/
*/

function XCommanderUIPanel() {
	this.ESC = 27;
	this.RETURN = 13;
	this.TAB = 9;
	this.xcommands = [];
	this.filetredXcommands = [];

	this.template = "<li><img src='{$icon}' align='absmiddle'/>{$name}</li>";
	this.input = $("#xcommander_ui_input");
	this.panel = $("#xcommander_ui_panel");
	this.list = $("#xcommander_ui_xcommands > ul");
	this.autoSuggestion = new XCommanderAutoSuggestion($("#xcommander_ui_suggest"), $("#xcommander_ui_input"))

	var help = $("#xcommander_ui_helpprompt");
	var preview = $("#xcommander_ui_preview");
	var self = this;
	var preview_text = "Gib ein XCommand in der Kommandozeile ein!";
	preview.html(preview_text);
	
	this.input.bind("keyup", function(event) {
		if(event.keyCode == self.RETURN) {
			if(self.filetredXcommands.length == 1) {
				self.filetredXcommands[0].execute(xcommander.parser.args, preview);
			}
		}
		if((event.keyCode == self.TAB) && (self.filetredXcommands.length == 1)) {
			var xcommand = self.filetredXcommands[0];
			xcommander.parser.parse(self.input.val(), xcommand);
			xcommand.preview(xcommander.parser.args, preview);
			help.html(xcommander.parser.echo.join(" "));
		}

		if(self.input.val() == "") {
			preview.html(preview_text);
			help.empty();
		}
	});

	this.input.bind("input", function() {
		if(self.filter()) {
			var xcommand = self.filetredXcommands[0];
			var regex = new RegExp("^" + xcommand.name);
			if(regex.test(self.input.val())) {
				xcommander.parser.parse(self.input.val(), xcommand);
				self.autoSuggestion.suggestions = xcommander.parser.currentSuggest;
				xcommand.preview(xcommander.parser.args, preview);
				help.html(xcommander.parser.echo.join(" "));
			}
		}else{
			preview.html(preview_text);
		}
	});

	xcommander.createXCommand = function(xcommand) {
		xcommander.ui.panel.xcommands.push(xcommand);
	}
}

XCommanderUIPanel.prototype = {
	_enabled : false,

	_onKeypress : function(event) {
		var keyCode = event.keyCode;
		var key = ($.browser.webkit) ? 21 : 85;
		var keyPattern = (keyCode == key) && (event.ctrlKey || event.metaKey) && event.shiftKey

		if(!this._enabled) {
			if(keyPattern) {
				this.xcommands = [];
				this.loadXCommands();
			}
		} else {
			if(keyPattern || keyCode == this.ESC) {
				this.toggle();
			}
		}
	},
	toggle : function() {
		this._enabled = !this._enabled;
		var self = this;
		this.panel.fadeToggle("fast", "linear", function() {
			self.input.focus();
		});
	},
	loadXCommands : function() {
		var self = this;
		xcommander.xcommand.getEnabled(function(data) {
			for(var i = 0; i < data.length; i++) {
				try {
					eval(data[i].xcommand);
				} catch(e) {
					// ignore xcommand !
				}
			}
			self.sortXCommands();
			self.addXCommands(self.xcommands);
			self.toggle();
		});
	},
	addXCommands : function(xcommands) {
		var count = 0;
		this.list.empty();
		for(var i = 0; i < xcommands.length; i++) {
			if(++count > 8) {
				if(count === 9) {
					this.list.append("<li>...</li>");
				}
				return;
			}
			var tmp = this.template.replace("{$icon}", xcommands[i].icon);
			tmp = tmp.replace("{$name}", xcommands[i].name);
			this.list.append(tmp);
		}
		$("#xcommander_ui_xcommands > ul > li:first").attr({
			"class" : "first"
		});
	},
	sortXCommands : function() {
		this.xcommands.sort(function(a, b) {
			if(a.name > b.name)
				return 1;
			if(a.name < b.name)
				return -1;
			return 0;
		});
	},
	filter : function() {
		var pattern = this.input.val();
		if(pattern.indexOf(" ") > 0) {
			pattern = pattern.substring(0, pattern.indexOf(" "));
		}
		this.filetredXcommands = [];
		for(var i = 0; i < this.xcommands.length; i++) {
			if(this.xcommands[i].name.match("^" + pattern)) {
				this.filetredXcommands.push(this.xcommands[i]);
			}
		}
		this.autoSuggestion.suggestions = this.getXCommands();
		this.addXCommands(this.filetredXcommands);
		return (this.filetredXcommands.length == 1);
	},
	getXCommands : function() {
		var xcommands = [];
		for(var i = 0; i < this.xcommands.length; i++) {
			xcommands.push(this.xcommands[i].name);
		}
		return xcommands;
	}
}

window.addEventListener('keypress', function(event) {
	if(xcommander.hasOwnProperty("ui")) {
		xcommander.ui.panel._onKeypress(event);
	}
}, false);
